package com.ubxtech.mqtt.config;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.ubxtech.core.utils.StrPool;
import org.springframework.integration.mqtt.inbound.AbstractMqttMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.outbound.AbstractMqttMessageHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Victor.Xiao
 * @since 2022-11-09 17:17
 **/
public class MqttBeanHolderUtil {

    /** 用于发送消息 */
    private final static Map<String, AbstractMqttMessageHandler> MESSAGE_HANDLER_MAP = new HashMap<>();
    /** 用于处理topic处理 */
    private final static Map<String, AbstractMqttMessageDrivenChannelAdapter> CHANNEL_ADAPTER_MAP = new HashMap<>();
    /**
     * 存放handler
     * @author Victor.Xiao
     * @since 2022-11-09 17:18
     * @param channelName channelName
     * @param handler handler
    */
    protected static void putMessageHandler(String channelName, AbstractMqttMessageHandler handler) {
        MESSAGE_HANDLER_MAP.put(channelName, handler);
    }
    /**
     * 存放adapter
     * @author Victor.Xiao
     * @since 2022-11-09 17:18
     * @param channelName channelName
     * @param adapter adapter
     */
    protected static void putAdapter(String channelName, AbstractMqttMessageDrivenChannelAdapter adapter) {
        CHANNEL_ADAPTER_MAP.put(channelName, adapter);
    }

    /**
     * 获取adapter
     * @author Victor.Xiao
     * @since 2022-11-10 11:26
     * @param channelName channelName
     * @return org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter
    */
    public static AbstractMqttMessageDrivenChannelAdapter getAdapter(String channelName) {
        return CHANNEL_ADAPTER_MAP.get(channelName);
    }

    /**
     * 获取adapter
     * @author Victor.Xiao
     * @since 2022-11-10 11:26
     * @param channelName channelName
     * @return org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler
    */
    public static AbstractMqttMessageHandler getMessageHandler(String channelName) {
        return MESSAGE_HANDLER_MAP.get(channelName);
    }

    /**
     * 看是否有MessageHandler
     * @author Victor.Xiao
     * @since 2022-11-10 11:42
     * @param channelName channelName
     * @return boolean
    */
    public static boolean hasMessageHandler(String channelName) {
        return MESSAGE_HANDLER_MAP.containsKey(channelName);
    }

    /**
     * 获取一个handler
     * @author Victor.Xiao
     * @since 2022-11-10 11:42
     * @return boolean
     */
    public static AbstractMqttMessageHandler getDefaultMessageHandler() {
        if (MESSAGE_HANDLER_MAP.size() > 0) {
            return MESSAGE_HANDLER_MAP.values().iterator().next();
        }
        return null;
    }

    /**
     * 获取一个adapter
     * @author Victor.Xiao
     * @since 2022-11-10 11:42
     * @return boolean
     */
    public static AbstractMqttMessageDrivenChannelAdapter getDefaultAdapter() {
        if (CHANNEL_ADAPTER_MAP.size() > 0) {
            return CHANNEL_ADAPTER_MAP.values().iterator().next();
        }
        return null;
    }

    /**
     * 看是否有adapter
     * @author Victor.Xiao
     * @since 2022-11-10 11:42
     * @param channelName channelName
     * @return boolean
     */
    public static boolean hasAdapter(String channelName) {
        return CHANNEL_ADAPTER_MAP.containsKey(channelName);
    }

    public static String appendClientId(String clientId,String clientIdFixed) {
        //为空,0,false都是随机生产
        if (StrUtil.isBlank(clientIdFixed) || StrPool.ZERO.equalsIgnoreCase(clientIdFixed) || StrPool.FALSE.equalsIgnoreCase(clientIdFixed)) {
            return clientId + "_" + RandomUtil.randomString(5);
        }
        return clientId;
    }
}
